package calculatorWithCss;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

// 简单计算器和CSS的使用和异常处理
public class CalculatorWithCSS extends Application { 
	private VBox root = new VBox(25);
	private HBox h1 = new HBox(30);
	private HBox h2 = new HBox(30);
	private HBox h3 = new HBox(30);
	private HBox buttonPane = new HBox(15);
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		// 第一个数面板
		Text firstNumber = new Text("firstNumber"); 
		firstNumber.setId("firstNumber");	// 用于ID选择器
		
		TextField tf1 = new TextField();
		HBox.setMargin(tf1, new Insets(0, 0, 0, 20));
		h1.getChildren().addAll(firstNumber, tf1);
		
		// 第二个数面板
		Text secondNumber = new Text("secondNumber");
		secondNumber.setId("secondNumber");
		
		TextField tf2 = new TextField();
		h2.getChildren().addAll(secondNumber, tf2);
		
		// 结果面板
		Text result = new Text("result");
		result.setId("result");
		
		TextField resultField = new TextField();
		HBox.setMargin(resultField, new Insets(0, 0, 0, 58));
		h3.getChildren().addAll(result, resultField);
		
		// 加减乘除按钮面板
		Button addButton = new Button("加");
		addButton.setOnMouseClicked(e -> add(tf1, tf2, resultField));
		
		Button subtractButton = new Button("减");
		subtractButton.setOnMouseClicked(e -> subtract(tf1, tf2, resultField));
		
		Button multiplyButton = new Button("乘");
		multiplyButton.setOnMouseClicked(e -> multiply(tf1, tf2, resultField));
		
		Button divideButton = new Button("除");
		divideButton.setOnMouseClicked(e -> divide(tf1, tf2, resultField));
		buttonPane.setAlignment(Pos.BOTTOM_RIGHT);
		buttonPane.getChildren().addAll(addButton, subtractButton, multiplyButton, divideButton);
		
		root.getChildren().addAll(h1, h2, h3, buttonPane);
		root.setPadding(new Insets(60));
		
		Scene scene = new Scene(root);
		scene.getStylesheets().add("CartoonDemo/Calculator.css");	// 使用CSS改变样式
		stage.setScene(scene);
		stage.setTitle("Calculator");
		stage.show(); 
	}
	
	// 加法
	private void add(TextField tf1, TextField tf2, TextField resultField) {
		try {
			 double a1 = Double.parseDouble(tf1.getText());
			 double a2 = Double.parseDouble(tf2.getText());
			 resultField.setText("" + (a1 + a2));
		} catch (Exception e) {
			System.out.println("输入异常");
		}
	}
	
	// 减法
	private void subtract(TextField tf1, TextField tf2, TextField resultField) {
		try {
			double a1 = Double.parseDouble(tf1.getText());
			double a2 = Double.parseDouble(tf2.getText());
			resultField.setText("" + (a1 - a2));
		} catch (Exception e) {
			System.out.println("输入异常");
		}
	}
	
	// 乘法
	private void multiply(TextField tf1, TextField tf2, TextField resultField) {
		try {
			double a1 = Double.parseDouble(tf1.getText());
			double a2 = Double.parseDouble(tf2.getText());
			resultField.setText("" + (a1 * a2));
		} catch (Exception e) {
			System.out.println("输入异常");
		}
	}
	
	// 除法
	private void divide(TextField tf1, TextField tf2, TextField resultField) {
		try {
			double a1 = Double.parseDouble(tf1.getText());
			double a2 = Double.parseDouble(tf2.getText());
			resultField.setText("" + (a1 / a2));
		} catch (Exception e) {
			System.out.println("输入异常");
		}
	}
}


